//
//  BookVC.m
//  AlumniBook
//
//  Created by 喂！ on 2018/10/30.
//  Copyright © 2018年 well. All rights reserved.
//

#import "BookVC.h"

@interface BookVC ()

@end

@implementation BookVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor greenColor];
    [self initFrame];
    
}

-(void)initFrame
{
    [self.view addSubview:self.mainTableView];
    [self.mainTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self initData];
}

-(void)initData
{
    NSMutableDictionary *requestDic = [[NSMutableDictionary alloc] init];
    [requestDic setValue:[[WellConfig sharedInstance].loginDic valueForKey:@"userGroup"] forKey:@"userGroup"];
    
    MoGetUserList *moGetUserList = [[MoGetUserList alloc] init];
    moGetUserList.baseHttpServiceBlock = ^(NSMutableDictionary *response) {
        NSInteger status = [[response valueForKey:@"Result"] integerValue];
        if (status == 0) {
            
            
            [self.mainTableView reloadData];
        }
        else
        {
            [self showRequestAlert:[response valueForKey:@"ErrMsg"]];
        }
    };
    [moGetUserList requestToService:requestDic];
}

#pragma mark - UITableViewDelegate and UITableDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    [tableView deselectRowAtIndexPath:indexPath animated:false];
//    [self closeView:[self.dataArray objectAtIndex:indexPath.row]];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellId = @"BookVCCell";
    BookVCCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[BookVCCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    NSMutableDictionary *dic = [self.dataArray objectAtIndex:indexPath.row];
    [cell setCellText:[dic valueForKey:@"FunctionName"]];
    
    return cell;
}


-(void)showRequestAlert:(NSString *)msg
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"错误" message:msg preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:action];
    [self presentViewController:alert animated:false completion:nil];
}




#pragma mark    懒加载
-(UITableView *)mainTableView
{
    if (!_mainTableView) {
        _mainTableView = [UITableView new];
        _mainTableView = [[UITableView alloc] init];
        _mainTableView.delegate = self;
        _mainTableView.dataSource = self;
        _mainTableView.estimatedRowHeight = 40;
        _mainTableView.rowHeight = UITableViewAutomaticDimension;
        _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mainTableView.backgroundColor = [UIColor clearColor];
    }
    return _mainTableView;
}

-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}


@end
